package com.example.zzb.my_lock;
import android.app.admin.DevicePolicyManager;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.ComponentName;
import android.content.Context;

public class MainActivity extends AppCompatActivity {
    private DevicePolicyManager mDevicepolicymanager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DevicePolicyManager mDevicepolicymanager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
        ComponentName mComponentname = new ComponentName(this, manager.class);

        if (mDevicepolicymanager.isAdminActive(mComponentname)) {
            mDevicepolicymanager.lockNow();
            System.exit(0);
        } else {// 第一次运行程序
            Intent intent = new Intent(
                    DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
            intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
                    mComponentname);
            intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
                    "One key lock screen need to active");
            startActivityForResult(intent, RESULT_OK);

        }
    }
}
